﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Data.Entity;
using Model;

namespace DataAccess
{
    public class BreakAwayContext:DbContext
    {
        public DbSet<Destination> Destinations { get; set; }
        public DbSet<Lodging> Lodgings { get; set; }
        public DbSet<Trip> Trips { get; set; }
        public DbSet<Person> Persons { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            /*
            modelBuilder.Entity<Destination>().ToTable("Destination");
            modelBuilder.Entity<Destination>().Property(l => l.Name).IsRequired().HasMaxLength(200);
            modelBuilder.Entity<Destination>().Property(d => d.Photo).HasColumnType("image");
            modelBuilder.Entity<Destination>().Property(d => d.Description).HasMaxLength(600);
            */
            modelBuilder.Configurations.Add(new DestinationConfiguration());
            modelBuilder.Configurations.Add(new LodgingConfiguration());
            modelBuilder.Configurations.Add(new TripConfiguration());
            modelBuilder.Configurations.Add(new PersonConfiguration());
            modelBuilder.Configurations.Add(new AddressConfiguration());
            modelBuilder.ComplexType<Address>();
            base.OnModelCreating(modelBuilder);
        }

        //将Fluent配置信息组织到对应的类里面
        public class DestinationConfiguration:EntityTypeConfiguration<Destination>
        {
            public DestinationConfiguration()
            {
                ToTable("Destination");
                Property(d => d.Name).IsRequired().HasMaxLength(200);
                Property(d => d.Description).HasMaxLength(600);
                Property(d => d.Photo).HasColumnType("image");
            }
        }

        public class LodgingConfiguration:EntityTypeConfiguration<Lodging>
        {
            public LodgingConfiguration()
            {
                Property(d => d.Name).HasMaxLength(200).IsRequired();
            }
        }

        public class TripConfiguration:EntityTypeConfiguration<Trip>
        {
            public TripConfiguration()
            {
                HasKey(t => t.Identifier)
                    .Property(t=>t.Identifier)
                    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
                Property(t => t.RowVersion)
                    .IsRowVersion();
            } 
        }

        public class PersonConfiguration:EntityTypeConfiguration<Person>
        {
            public PersonConfiguration()
            {
                /*HasKey(t => t.SocialSecurityNumber)
                    .Property(t=>t.SocialSecurityNumber)
                    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
                Property(d => d.RowVersion)
                    .IsRowVersion();*/
                Property(t => t.SocialSecurityNumber).IsConcurrencyToken();
                Property(t => t.Address.City).HasColumnName("Add_City");
            }
        }

        public class AddressConfiguration:ComplexTypeConfiguration<Address>
        {
            public AddressConfiguration()
            {
                Property(p => p.StreetAddress).HasMaxLength(32);
            }
        }
    }
}
